<template>
  <div class="page-container">
    <h2>新增专栏课程</h2>
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" class="form-card">
      <el-form-item label="专栏信息" prop="columnInfo">
        <el-input v-model="formData.columnInfo" type="textarea" :rows="3" placeholder="请输入专栏详细信息" />
      </el-form-item>
      <el-form-item label="专栏价格" prop="columnPrice">
        <el-input-number v-model="formData.columnPrice" :min="0" :precision="2" style="width: 100%" />
      </el-form-item>
      <el-form-item label="订阅数" prop="subscribeNum">
        <el-input-number v-model="formData.subscribeNum" :min="0" />
      </el-form-item>
      <el-form-item label="课程数量" prop="columnCourseNum">
        <el-input-number v-model="formData.columnCourseNum" :min="0" />
      </el-form-item>
      <el-form-item label="专栏状态" prop="columnState">
        <el-radio-group v-model="formData.columnState">
          <el-radio :label="1">上架</el-radio>
          <el-radio :label="0">下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" :loading="submitting" @click="handleSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { columnApi } from '@/api/column'

const formRef = ref()
const submitting = ref(false)

const formData = reactive({
  columnInfo: '',
  columnPrice: 0,
  subscribeNum: 0,
  columnCourseNum: 0,
  columnState: 1
})

const formRules = {
  columnInfo: [{ required: true, message: '请输入专栏信息', trigger: 'blur' }],
  columnPrice: [{ required: true, message: '请输入专栏价格', trigger: 'blur' }],
  columnState: [{ required: true, message: '请选择专栏状态', trigger: 'change' }]
}

const handleReset = () => {
  Object.assign(formData, { columnInfo: '', columnPrice: 0, subscribeNum: 0, columnCourseNum: 0, columnState: 1 })
}

const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    submitting.value = true
    await columnApi.addColumn(formData)
    ElMessage.success('新增成功')
    handleReset()
  } catch (e) {
    if (e !== false) ElMessage.error('新增失败')
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped>
.page-container { padding: 20px; }
.form-card { max-width: 600px; }
</style>


